﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.Models;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Infrastructure;
using System.ComponentModel.DataAnnotations;

namespace MiniFox.Platform.OAuth2.Web
{
    /// <summary>
    /// 
    /// </summary>
    [AllowAnonymous, NoCache]
    public class AccessTokenController : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        protected virtual IAccessTokenService Component
        {
            get
            {
                return this.Container.GetComponent<IAccessTokenService>();
            }
        }
        /// <summary>
        /// 颁发token
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="secretKey"></param>
        /// <param name="authCode"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<AuthorizeToken> Issue(
            [FromQuery(Name = OpenAuthKey.AUTH_APP_ID), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string appId,
            [FromQuery(Name = OpenAuthKey.AUTH_SECRET_KEY), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string secretKey,
            [FromQuery(Name = OpenAuthKey.AUTH_CODE), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string authCode
            )
        {
            var token = this.Component.CreateAuthorizeToken(appId, secretKey, authCode);
            return token.CreateResponse();
        }
        /// <summary>
        /// 续证
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="secretKey"></param>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<AuthorizeToken> Renew(
            [FromQuery(Name = OpenAuthKey.AUTH_APP_ID), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string appId,
            [FromQuery(Name = OpenAuthKey.AUTH_SECRET_KEY), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string secretKey,
            [FromQuery(Name = OpenAuthKey.AUTH_REFRESH_TOKEN), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string refreshToken
            )
        {
            var token = this.Component.RenewAuthorizeToken(appId, secretKey, refreshToken);
            return token.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<OpenAuthIdentity> Identity([FromQuery(Name = OpenAuthKey.AUTH_ACCESS_TOKEN), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string accessToken)
        {
            var identity = this.Component.GetIdentity(accessToken);
            return identity.CreateResponse();
        }
    }
}
